//
//  CommunicatieViewController.swift
//  willdo
//
//  Created by zz on 4/19/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class CommunicatieViewController: UIViewController {
    let scrollView = UIScrollView()
    
    private var tmpView:UIView!
    private var tmpCellView:UIView!

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        if (tmpView.frame.maxY > 10){
            scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: tmpView.frame.maxY+30)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = BACKGROUND_GRAY
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        scrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height)
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        let topView = UIView()
        topView.backgroundColor = UIColor.clear
        scrollView.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.top.equalTo(scrollView)
            make.height.equalTo(0)
            make.left.right.equalTo(view)
        }
        tmpView = topView
        for modle in COMMODLE{
            let comView = setupCellView(modle: modle)
            scrollView.addSubview(comView)
            comView.snp.makeConstraints { (make) in
                make.left.equalTo(view).offset(10)
                make.right.equalTo(view).offset(-10)
                make.top.equalTo(tmpView.snp.bottom).offset(15)
                make.bottom.equalTo(tmpCellView).offset(10)
            }
            tmpView = comView
        }
        
    }
    
    func setupCellView(modle: CommunicatModle) -> UIView {
        let cellView = UIView()
        let AvatarImage = UIButton()
        let LeaderSpeak = UIButton()
        let StartTime = UILabel()
        let TaskMsg = UILabel()
        
        cellView.backgroundColor = UIColor.white
        cellView.addSubview(AvatarImage)
        cellView.addSubview(LeaderSpeak)
        cellView.addSubview(StartTime)
        cellView.addSubview(TaskMsg)
        
        AvatarImage.setImage(UIImage(named:"head portrait"), for: .normal)
        AvatarImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(11)
            make.left.equalToSuperview().offset(11)
            make.width.height.equalTo(29)
        }
        
        let attributeTitle = NSAttributedString(string: modle.name, attributes:
            [NSAttributedStringKey.font : UIFont(name: "PingFang SC", size: 15)!,
             NSAttributedStringKey.foregroundColor:UIColor(hexString:"#040000")])
        LeaderSpeak.setAttributedTitle(attributeTitle, for: .normal)
        LeaderSpeak.sizeToFit()
        LeaderSpeak.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage).offset(-2)
            make.left.equalTo(AvatarImage.snp.right).offset(8)
            make.height.equalTo(22)
        }
        
        StartTime.text = modle.commitTime
        StartTime.font = UIFont.init(name: "PingFang SC", size: 9)
        StartTime.textColor = UIColor(hexString: "#929292")
        StartTime.snp.makeConstraints { (make) in
            make.bottom.equalTo(AvatarImage).offset(2)
            make.left.equalTo(LeaderSpeak).offset(3)
        }
        
        TaskMsg.text = modle.commitMsg
        TaskMsg.numberOfLines = 0
        TaskMsg.textAlignment = .justified
        TaskMsg.textColor = UIColor(hexString:"040000")
        TaskMsg.font = UIFont(name: "PingFang SC", size: 13)
        
        TaskMsg.sizeToFit()
        TaskMsg.snp.makeConstraints { (make) in
            make.top.equalTo(AvatarImage.snp.bottom).offset(11)
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
        }
        
        let files = modle.file
        tmpCellView = TaskMsg
        
        if files != nil {
            for file in files!{
                let fileView = FileView(file: file)
                cellView.addSubview(fileView)
                fileView.snp.makeConstraints({ (make) in
                    make.top.equalTo(tmpCellView.snp.bottom).offset(4)
                    make.left.right.equalTo(tmpCellView)
                    make.height.equalTo(80)
                })
                tmpCellView = fileView
            }
            
        }

        return cellView
    }
    
    
}

let comModle0 = CommunicatModle(avatar: "123", name: "张三", commitTime: "2018.1.1", commitMsg: "OK，没问题，通过", file: nil)
let comModle1 = CommunicatModle(avatar: "123", name: "李四", commitTime: "2018.1.1", commitMsg: "图已经画完了，你看一下还有没有问题", file: [file2])
let comModle2 = CommunicatModle(avatar: "123", name: "张三", commitTime: "2018.1.1", commitMsg: "帆帆，下个月就是十佳大学生评选了，组织部这边要先行动起来，咱们这次的场地选在了学活剧院，需要预留50位嘉宾领导的位子，其余作为观众位，至于工作人员和候选人员的作为安排，你们自己考虑下。12月25日之前务必提交一版作为方案，用excel就可以了。", file: nil)
//let comModle3 = CommunicatModle(avatar: "123", name: "姓名", commitTime: "2018.1.1", commitMsg: "就是这个信息", file: nil)
//let comModle4 = CommunicatModle(avatar: "123", name: "姓名", commitTime: "2018.1.1", commitMsg: "就是这个信息", file: nil)

let COMMODLE = [comModle0,comModle1,comModle2]
